require 'rbof'

class App < OF::SimpleApp
  include OF
  
  def setup
    setVerticalSync true
    
    @trans_img = Image.new
    @trans_img.loadImage "transparency.png"

    @noise_img = Image.new
    @noise_w, @noise_h = 100, 100
    @noise_img.allocate @noise_w, @noise_h, IMAGE_GRAYSCALE
    @pixels = Array.new @noise_w * @noise_h
    
    @msg = "click to capture screen"
  end
  
  def update
    @pixels.map! {rand 255}
    @noise_img.setFromPixels @pixels, @noise_w, @noise_h, IMAGE_GRAYSCALE
  end

  def draw
    w, h = getWidth, getHeight
    @noise_img.draw 0, 0, w, h
    
    drawBitmapString @msg, 20, 40
    
    translate 0.5 * w, 0.5 * h, 0
    pushMatrix
      rotate getFrameNum * 2, 0, 1, 0
      enableAlphaBlending
        @trans_img.draw(
          -0.5 * @trans_img.width + 50, 
          -0.5 * @trans_img.height
        )
      disableAlphaBlending
    popMatrix
  end
  
  def mousePressed(x, y, button)
    save_img = Image.new
    save_img.grabScreen 0, 0, getWidth, getHeight
    file_name = "snapshot.png"
    save_img.saveImage file_name
    
    @msg = "done! see #{toDataPath file_name}"
  end
end

OF.setupOpenGL 400, 400, OF::WINDOW
OF.runApp App.new